package tp.pr4.instructions;

import java.util.StringTokenizer;
import tp.pr4.NavigationModule;
import tp.pr4.RobotEngine;
import tp.pr4.instructions.exceptions.InstructionExecutionException;
import tp.pr4.instructions.exceptions.WrongInstructionFormatException;
import tp.pr4.items.ItemContainer;

/**
 * Shows the game help with all the instructions that the robot can execute. 
 * This instruction works if the user writes HELP or AYUDA
 * @author Alvaro Saez Hernando
 * @author Noel Clemente Montero 
 */
public class HelpInstruction implements Instruction{
	
	private static final String LINE_SEPARATOR = System.lineSeparator();
	
	private RobotEngine robotEngine;
	
	/**
	 * Default constructor.
	 */
	public HelpInstruction(){
	}
	
	/**
	 * Parses the String returning a HelpInstruction instance or throwing a 
	 * WrongInstructionFormatException()
	 * @param cad Text String to parse
	 * @return Instruction Reference to an instance of HelpInstruction
	 * @throws WrongInstructionFormatException When the String is not HELP
	 */
	public Instruction parse(java.lang.String cad)throws WrongInstructionFormatException{
		
		StringTokenizer st = new StringTokenizer(cad, " ");
		String word = st.nextToken().toUpperCase();
		
		if ((word.equals("HELP")) || (word.equals("AYUDA"))) {
			if (!st.hasMoreTokens())
				return new HelpInstruction();
			else
				throw new WrongInstructionFormatException("WALL·E says: I do not understand. Please repeat" + LINE_SEPARATOR);
		} 
		else
			throw new WrongInstructionFormatException("WALL·E says: I do not understand. Please repeat" + LINE_SEPARATOR);
		
	}

	/**
	 * Help syntax
	 * @return the instruction syntax HELP
	 */
	public String getHelp() {
		return "HELP|AYUDA" + LINE_SEPARATOR;
	}

	/**
	 * Configuration of the context for this instruction
	 * @param engine The robot engine
	 * @param navigation The information about the game, i.e., the places, current 
	 * direction and current heading to navigate
	 * @param robotContainer The inventory of the robot
	 */
	public void configureContext(RobotEngine engine,NavigationModule navigation, ItemContainer robotContainer) {
		this.robotEngine = engine;
	}

	/**
	 * Prints the help string of every instruction. It delegates to the Interpreter class.
	 * @throws InstructionExecutionException if there exist any execution error.
	 */
	public void execute() throws InstructionExecutionException {
		robotEngine.requestHelp();
	}

}
